// component/wallet/list.js
const app = getApp()

import {
        summary
} from '../../api/card'
import {
        list
} from '../../api/virtualStock'
import {
        getInfo,
} from '../../api/member'
Component({
        /**
         * 组件的属性列表
         */
        properties: {

        },

        /**
         * 组件的初始数据
         */
        data: {
                balance: '0.00', //账户余额
                benefits: '0.00', //福利金
                point: 0,
                virtualStock: 0,
                couponCode: 0,
                matchPoint: 0,
                waterPrice:0,
                warehouse:'0',
        },

        lifetimes: {
                attached: function () {
                        // 在组件实例进入页面节点树时执行
                        //可视窗口x,y坐标
                        // this.getSummary()
                },
                detached: function () {
                        // 在组件实例被从页面节点树移除时执行
                },
        },

        pageLifetimes: {
                show: function () {
                        // 页面被展示
                        console.info("页面被展示了")
                        this.setData({
                                benefits: this.data.benefits,
                                VERSION: app.VERSION
                        })
                        this.getSummary()
                        this.getInfo()
                        //this.getVirtualStock()
                },
                hide: function () {
                        // 页面被隐藏
                },
                resize: function (size) {
                        // 页面尺寸变化
                }
        },

        /**
         * 组件的方法列表
         */
        methods: {

                //请求用户的账户卡和福利金的余额数据
                getSummary() {
                        console.log("请求用户的账户卡和福利金的余额数据--")
                        summary().then(
                                res => {
                                        console.log("接收用户信息--" + JSON.stringify(res))
                                        if (res.type == 'success') {
                                                if (res.hasOwnProperty("data")) {
                                                                if (res.data.hasOwnProperty("benefits")) {
                                                                        this.setData({
                                                                                benefits: res.data.benefits.toFixed(2)
                                                                        })
                                                                }
                                                        if (res.data.hasOwnProperty("balance")) {
                                                                this.setData({
                                                                        balance: res.data.balance.toFixed(2)
                                                                })
                                                        }


                                                }
                                        }
                                }
                        ).catch(error => {
                                console.log(error)
                        })
                },

                //获取用户信息
                getInfo() {
                        var _this = this
                        getInfo().then(
                                res => {
                                        console.log(res)
                                        if (res.type == 'success') {
                                                this.setData({
                                                        point: res.data.point,
                                                        virtualStock: res.data.virtualStock,
                                                        couponCode: res.data.couponCode,
                                                        matchPoint: res.data.matchPoint,
                                                        warehouse:res.data.balance
                                                })
                                                console.log(res.data.point)
                                        }
                                }
                        ).catch(error => {
                                console.log(error)
                        })
                },
                // getVirtualStock() {
                //         list({
                //                 pageSize: 1000,
                //                 subType: '3'
                //         }).then(res => {
                //                 this.data.waterPrice = 0;
                //                 res.data.data.forEach(item => {
                //                         this.data.waterPrice += Number(item.price)
                //                 })
                //                 this.setData({
                //                         waterPrice: this.data.waterPrice.toFixed(2)
                //                 })
                //         })
                // },
                //跳转到账户余额的页面

                toSaveCard: function (e) {
                        this.checkLogin(() => {
                                wx.navigateTo({
                                        url: '/pages/member/saveCard/index',
                                        success: function (res) { },
                                        fail: function (res) { },
                                        complete: function (res) { },
                                })
                        })
                },

                toPione: function (e) {
                        this.checkLogin(() => {
                                wx.navigateTo({
                                        url: '/pages/member/distribution/index',
                                })
                        })
                },
                // 跳转到库存
                toWarehouse: function (e) {
                        this.checkLogin(() => {
                                wx.navigateTo({
                                        url: '/pages/member/warehouse/warehouse?balance='+this.data.warehouse,
                                })
                        })
                },
                //跳转到福利金的页面
                toShopCard: function (e) {
                        if (app.VERSION === 0 || app.VERSION === 2) {
                                this.checkLogin(() => {
                                        wx.navigateTo({
                                                url: '/pages/member/shopCard/shopCard',
                                                success: function (res) { },
                                                fail: function (res) { },
                                                complete: function (res) { },
                                        })
                                })
                        } else if (app.VERSION === 1) {
                                this.checkLogin(() => {
                                        wx.navigateTo({
                                                url: '/pages/member/emptyBarrel/emptyBarrel',
                                                success: function (res) { },
                                                fail: function (res) { },
                                                complete: function (res) { },
                                        })
                                })
                        }
                },
                toVouchers() {
                        this.checkLogin(() => {
                                wx.navigateTo({
                                        url: '/pages/member/couponCodeGift/index?currentTab=0',
                                })
                                // wx.navigateTo({
                                //         url: '/pages/member/coupon/coupon?currentTab=2',
                                // })
                        })
                },
                toIntegral() {
                        this.checkLogin(() => {
                                wx.navigateTo({
                                        url: '/pages/member/integral/integral',
                                })
                        })
                },
                // toWater() {
                //         this.checkLogin(() => {
                //                 wx.switchTab({
                //                         url: '/pages/tabBar/order/water'
                //                 })
                //         })
                // },
                //检测用户的登陆状态
                checkLogin(callback) {
                        console.info("全局变量app.globalData--" + JSON.stringify(app.globalData))
                        if (app.globalData.token == null) {
                                wx.navigateTo({
                                        url: '/pages/login/index',
                                })
                        } else {
                                if (callback) {
                                        callback()
                                }
                        }
                },
        }
})
